package com.gitee.kamismile.stone.basic.data.infrastructure.repository.handler.r2.mapping;

import org.springframework.data.convert.CustomConversions;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.r2dbc.convert.MappingR2dbcConverter;
import org.springframework.data.r2dbc.convert.R2dbcCustomConversions;
import org.springframework.data.r2dbc.mapping.OutboundRow;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.r2dbc.core.Parameter;

import java.util.Iterator;
import java.util.Objects;

public class CustomMappingR2dbcConverter extends MappingR2dbcConverter {

    public CustomMappingR2dbcConverter(MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context, CustomConversions conversions) {
        super(context, conversions);
    }

    @Override
    public void write(Object source, OutboundRow sink) {
        super.write(source, sink);
        Iterator<SqlIdentifier> iterator = sink.keySet().iterator();
        while (iterator.hasNext()){
            Parameter parameter = sink.get(iterator.next());
            if(Objects.isNull(parameter.getValue())){
                iterator.remove();
            }
        }

    }
}
